package uiTest;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.contexticon.SearchDemo.TableButton;


public class SwingEmbedding extends JFrame 
{
	private static final long serialVersionUID = 6788933562312584275L;

	@SuppressWarnings({ "unchecked", "serial" })
	public SwingEmbedding()
	{
		initComponents();
		
		// important to set the columns that will have embedded elements as "Editable"
		DefaultTableModel model = new DefaultTableModel(new String [] {"Button" }, 0) 
		{
	            Class[] types = new Class[]
	            {
	                  java.lang.String.class
	            };

	            public Class getColumnClass(int columnIndex)
	            {
	                return types[columnIndex];
	            }

	            boolean[] canEdit = new boolean [] {
	                 true
	            };

	            public boolean isCellEditable(int rowIndex, int columnIndex) {
	                return canEdit [columnIndex];
	            }
		};
		table.setModel(model);
		

        
		TableColumn buttonColumn = table.getColumnModel().getColumn(0);

        TableButton buttons = new TableButton();
        buttons.addHandler(new TableButton.TableButtonPressedHandler() {
			
			@Override
			public void onButtonPress(int row, int column) {
				// TODO Auto-generated method stub
				
			}
		});
        
        buttonColumn.setCellRenderer(buttons);
        buttonColumn.setCellEditor(buttons);
	}

	public void addElement()
	{
		DefaultTableModel model = (DefaultTableModel) table.getModel();	
		model.addRow(new Object[] { "Hello" });
	}

	/**
	 * Initializes components, code generated by NetBeans GUI editor
	 */
	private void initComponents() 
	{
		setTitle("Swing Embedding Demo");
		
		jScrollPane1 = new javax.swing.JScrollPane();
		table = new javax.swing.JTable();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jScrollPane1.setViewportView(table);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300,
				Short.MAX_VALUE));

		pack();
	}

	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable table;

	public static void main(String[] args) 
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		SwingEmbedding embedding = new SwingEmbedding();
		embedding.setVisible(true);
		
		embedding.addElement();
		embedding.addElement();
	}

}
